//app.js
App({
	onLaunch: function() {
		// 展示本地存储能力
		var logs = wx.getStorageSync('logs') || []
		logs.unshift(Date.now())
		wx.setStorageSync('logs', logs)
		var _this = this
		//获取手机型号
		wx.getSystemInfo({
			success: res => {
				var modelmes = res.model;
				console.log(modelmes)
				if (modelmes.search('iPhone X') != -1 || modelmes.search('iPhone XR') != -1 || modelmes.search('iPhone 11') !=
					-1 || modelmes.search('iPhone 11 Pro Max') != -1) {
					_this.globalData.isIphoneX = true
				}

			}
		})
		// 获取用户信息
		wx.getSetting({
			success: res => {
				console.log(res)
				if (res.authSetting['scope.userInfo']) {
					// 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
					wx.getUserInfo({
						success: r => {
							// 可以将 res 发送给后台解码出 unionId
							this.globalData.userInfo = r.userInfo
							// 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
							// 所以此处加入 callback 以防止这种情况
							if (this.userInfoReadyCallback) {
								this.userInfoReadyCallback(r)
							}
						}
					})
				}
			}
		})
		this.randomBox()
	},
	globalData: {
		userInfo: null,
		openId: '',
		mobile: '',
		session_key: '',
		boxArr: [],
		bBoxIdx: null,
		sBoxIdx: null,
		isIphoneX: false,
		ficationLOGO: '',
		goodsImg: {
			'龙女战士-经典黑': '2',
			'龙女战士-美国小丑': '3',
			'龙女战士-熊猫宝宝': '5',
			'龙女战士-黑手党': '7',
			'龙女战士-鼠年限定隐藏款': '8',
			'龙女战士-希腊女神': '9',
			'龙女战士-经典红': '10'
		},
		url:'https://www.youjingxi.com.cn',
		//url: 'http://www.youjingxi.net.cn'
	},
	goPage: function(e) {
		var pages = getCurrentPages() //获取加载的页面
		var currentPage = pages[pages.length - 1] //获取当前页面的对象
		var url = currentPage.route //当前页面url
		var targetPage = e.currentTarget.dataset.url.substring(1)
		if (url != targetPage) {
			wx.navigateTo({
				url: '/' + targetPage
			})
		}
	},
	getUserMsg: function() {
		var _this = this
		wx.request({
			url: '',
			method: 'GET',
			data: this.globalData.openId,
			success: function(r) {

			}
		})
	},
	// 随机生成盒子
	randomBox: function() {
		var arr = []
		var allBox = []
		for (var b = 0; b < 9; b++) {
			for (var i = 0; i < 9; i++) {
				var num = Math.floor(Math.random() * 2)
				arr.push(num)
			}
			allBox.push(arr)
			arr = []
		}
		this.globalData.boxArr = allBox
		console.log('app-----boxArr----', this.globalData.boxArr)
	},
	//登录
	login: function() {
		const _this = this
		return new Promise(function(resolve, reject) {
			wx.login({
			    success: res => {
			        // 发送 res.code 到后台换取 openId, sessionKey, unionId
			        wx.request({
			            url: _this.globalData.url+'/v1/wechat/accesstoken',
			            method: 'get',
			            data: {
			                code: res.code
			            },
			            success: function(r) {
			                console.log(r)
			                if (r.data.code === 0) {
			                    _this.globalData.session_key = r.data.data.session_key
			                    _this.globalData.openId = r.data.data.openid
								resolve(r.data)
			                    console.log(_this.globalData.session_key)
			                    console.log(_this.globalData.openId)
			                } else {
			                    wx.showToast({
			                        title: '获取信息失败',
			                        icon: 'none',
			                        duration: 2000
			                    })
			                }
			            },fail:function(err){
							reject(err);
						}
			        })
			    }
			})
			
		});
	}
})
